import { hiddenActiveListKey, showActiveListKey, fetchPageData, searchSessionFormData, searchSessionTpl } from './hylist'
import { changeFormKeyRules, clearFormRuleObj, resetNotRequiredRuleObj } from './hyformrules'
import { changeHiddenActiveRowCol, changeShowActiveRowCol, clearFormKeyVal, setFormTriggerObjVal } from './hyform'
import { processEl } from './auth'
import { changeActiveEnumKey, changeLoadEnumList } from '@/utils/hyenum'
import { changeFormBtn, hiddenActiveBtnKey, showActiveBtnKey } from '@/utils/hybtn'
import { getRefOpenObj } from '@/utils/hyopen'
import { triggerBtnEvent } from '@/utils/hyasync'
import { getGlobalParam } from './hyrecord'
import { showActiveTabKey,showHiddenTabKey } from './hytabs'

/**
 * 模板对象
 * @param thisObj
 * @param tplKey
 * @returns {*}
 */
export function getTplKeyObj(thisObj, tplKey) {
  const tplKeyArr = thisObj.$refs[tplKey]
  let tplKeyObj = {}
  if (tplKeyArr instanceof Array) {
    tplKeyObj = thisObj.$refs[tplKey][0]
  } else {
    tplKeyObj = thisObj.$refs[tplKey]
  }
  return tplKeyObj
}

export function getCallFnByFnType(btnItem) {
  if (btnItem.fnType === 99 || btnItem.fnType === 13) {
    // 个人配置按钮
    return 'searchSessionTplData'
  } else if (btnItem.fnType === 98) {
    // 关闭窗口按钮
    return 'btnClickEvent'
  } else if (btnItem.fnType === 94) {
    return 'searchInitData'
  } else if (btnItem.fnType === 95) {
    return 'searchSessionFormData'
  } else if (btnItem.fnType === 96) {
    return 'searchSessionFormData'
  } else if (btnItem.fnType === 97 || btnItem.fnType === 93 || btnItem.fnType === 92) {
    // 关闭窗口按钮
    return 'btnClickEvent'
  } else if (btnItem.fnType === 23) {
    return 'btnClickEvent'
  } else if (btnItem.fnType === 22) {
    // 批量本地删除
    return 'executeBatchLocalDelete'
  } else if (btnItem.fnType === 21) {
    // 本地删除
    return 'executeLocalDelete'
  } else if (btnItem.fnType === 16) {
    // 远程批量删除
    return 'executeBatchRemoteDelete'
  } else if (btnItem.fnType === 15) {
    // 远程删除
    return 'executeRemoteDelete'
  } else if (btnItem.fnType === 19) {
    // 暂存
    return 'executeTmpSave'
  } else if (btnItem.fnType === 12) {
    // 自定义配置
    return 'openSessionGeneralModal'
  } else if (btnItem.fnType === 11) {
    // 本地事件
    return 'executeLocalEvent'
  } else if (btnItem.fnType === 10) {
    // 下载类FTP函数
    // return 'downloadFTPFile'
    return 'btnClickEvent'
  } else if (btnItem.fnType === 9) {
    // 上报FTP文件类函数
    // return 'uploadFTPFile'
    return 'btnClickEvent'
  } else if (btnItem.fnType === 8) {
    // 下载类函数
    return 'downloadUploadFile'
  } else if (btnItem.fnType === 7) {
    return 'changeUploadFile'
  } else if (btnItem.fnType === 6) {
    return 'changeToOtherQuery'
  } else if (btnItem.fnType === 5) {
    return 'importFileData'
  } else if (btnItem.fnType === 4) {
    return 'exportFileData'
  } else if (btnItem.fnType === 3 || btnItem.fnType === 17) {
    return 'execute'
  } else if (btnItem.fnType === 2) {
    return 'openGeneralModal'
  } else if (btnItem.fnType === 1) {
    return 'searchQuery'
  } else {
    return 'btnClickEvent'
    // return 'searchQuery'
  }
}

export function inputChangeEvent(thisObj, formItem) {
  thisObj.globalData.formModel = thisObj.formModel
  thisObj.globalData.globalParam = getGlobalParam(thisObj)
  if (formItem.formBtn !== undefined) {
    changeFormBtn(thisObj, formItem)
  }
  if (undefined !== formItem.eventList) {
    // const record = Object.assign({}, thisObj.openParam, thisObj.initData, thisObj.formModel)
    inputChangeEventControlObj(thisObj, formItem, thisObj.globalData)
  }
  // 前一步事件处理，有可能会设置值或清空表单数据
  // const newRecord = Object.assign({}
  const newGlobalData = Object.assign({}, thisObj.globalData)
  newGlobalData.openParam = {}
  console.log('new global data', newGlobalData)
  if (undefined !== formItem.refOpenList) {
    eventOpenGeneralModal(thisObj, formItem, newGlobalData)
  }
  if (undefined !== formItem.refExport) {
    eventOpenGeneralModal(thisObj, formItem, newGlobalData)
  }
  if (undefined !== formItem.refImport) {
    eventOpenGeneralModal(thisObj, formItem, newGlobalData)
  }
}

export function eventOpenGeneralModal(thisObj, formItem, globalData) {
  const refOpenObj = getRefOpenObj(thisObj, formItem, globalData)
  if (refOpenObj !== null) {
    formItem.refOpen = refOpenObj
    thisObj.inputOpenGeneralModal(formItem, globalData)
  }
}

export function triggerEventOpenGeneralModal(thisObj, triggerObj, globalData) {
  const newItem = Object.assign({})
  newItem.refOpenList = triggerObj.refOpenList
  eventOpenGeneralModal(thisObj, newItem, globalData)
}

/**
 * 事件触发控制中心
 * @param thisObj 当前对象
 * @param formItem 操作项目
 * @param globalData 记录数据
 */
export function inputChangeEventControlObj(thisObj, formItem, globalData) {
  const eventList = formItem.eventList
  for (const eventIdx in eventList) {
    const eventData = eventList[eventIdx]
    inputChangeEventControlItemObj(thisObj, formItem, eventData, globalData)
  }
}

/**
 * 事件触发控制中心
 * @param thisObj 当前对象
 * @param formItem 操作项目
 * @param eventData
 * @param globalData 记录数据
 */
export function inputChangeEventControlItemObj(thisObj, formItem, eventData, globalData) {
  const triggerObjList = eventData.triggerObjList
  if (triggerObjList !== undefined && triggerObjList.length > 0) {
    for (const triggerIdx in triggerObjList) {
      const triggerObj = triggerObjList[triggerIdx]
      if (triggerObj.objEl !== undefined) {
        const elFlag = processEl(globalData.globalParam, triggerObj.objEl)
        if (!elFlag) {
          continue
        }
      }
      const showKey = triggerObj.s_RULE_PANEL_SHOW_KEY
      if (thisObj.initTableKey === showKey || showKey === '') {
        // 判断当前面板和要触发事件需求在同一个面板
        const busiGrpList = triggerObj.busiGrpList
        if (busiGrpList !== undefined && busiGrpList.length > 0) {
          for (const busiGrpIdx in busiGrpList) {
            const busiGrpObj = busiGrpList[busiGrpIdx]
            if (busiGrpObj.objEl !== undefined) {
              const elFlag = processEl(globalData.globalParam, busiGrpObj.objEl)
              if (!elFlag) {
                continue
              }
            }
            const itemList = busiGrpObj.itemList
            if (itemList !== undefined && itemList.length > 0) {
              for (const itemIdx in itemList) {
                const itemObj = itemList[itemIdx]
                if (itemObj.objEl !== undefined) {
                  const elFlag = processEl(globalData.globalParam, itemObj.objEl)
                  if (!elFlag) {
                    continue
                  }
                }
                inputChangeTriggerObjControlObj(thisObj, formItem, globalData, itemObj)
              }
            }
          }
        } else {
          inputChangeTriggerObjControlObj(thisObj, formItem, globalData, triggerObj)
        }
      } else {
        console.log('需执行其他面板执行', triggerObj)
        thisObj.changeUiEventTrigger(formItem, triggerObj)
      }
    }
  }
}
/**
 * 触发控制中心
 * @param thisObj 当前对象
 * @param formItem 操作项目
 * @param globalData 记录数据
 * @param triggerObj 触发对象信息
 */
export function inputChangeTriggerObjControlObj(thisObj, formItem, globalData, triggerObj) {
  console.log('inputChangeTriggerObjControlObj', triggerObj)
  const I_TRIGGER_OBJ = triggerObj.i_TRIGGER_OBJ
  const key = triggerObj.s_RULE_TPL_KEY
  if (I_TRIGGER_OBJ === 48) {
    // 标签组件
    const I_RULE_TPL_COMP = triggerObj.i_RULE_TPL_COMP
    // 标签属性
    const I_RULE_TPL_PROP = triggerObj.i_RULE_TPL_PROP

    // 标签变量
    const S_RULE_TPL_KEY = triggerObj.s_RULE_TPL_KEY
    if (I_RULE_TPL_COMP === 4) {
      // 标签显示或隐藏
      if (I_RULE_TPL_PROP === 1 || I_RULE_TPL_PROP === 15) {
        // 显示
        showActiveTabKey(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 2) {
        // 隐藏
        showHiddenTabKey(thisObj, S_RULE_TPL_KEY, globalData)
      }
    } else if (I_RULE_TPL_COMP === 3) {
      // const record = Object.assign({}, thisObj.openParam, thisObj.initData, thisObj.formModel, thisObj.clickRow)
      // 列表组件
      if (I_RULE_TPL_PROP === 1) {
        // 显示
        showActiveListKey(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 2) {
        // 隐藏
        hiddenActiveListKey(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 15) {
        // 显示或隐藏
        showActiveListKey(thisObj, S_RULE_TPL_KEY, globalData)
      }
    } else if (I_RULE_TPL_COMP === 2) {
      // 按钮组件
      if (I_RULE_TPL_PROP === 49) {
        execClickBtn(thisObj, globalData, S_RULE_TPL_KEY)
      } else if (I_RULE_TPL_PROP === 1) {
        // 按钮显示
        showActiveBtnKey(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 2) {
        // 按钮隐藏
        hiddenActiveBtnKey(thisObj, S_RULE_TPL_KEY, globalData)
      }
    } else if (I_RULE_TPL_COMP === 1) {
      // 表单
      if (I_RULE_TPL_PROP === 4) {
        // 清空
        clearFormKeyVal(thisObj, S_RULE_TPL_KEY, undefined)
      } else if (I_RULE_TPL_PROP === 3) {
        // 赋值
        setFormTriggerObjVal(thisObj, triggerObj)
      } else if (I_RULE_TPL_PROP === 5) {
        // 可写
        setTriggerObjModalFn(thisObj, triggerObj)
        changeFormKeyRules(thisObj, S_RULE_TPL_KEY)
      } else if (I_RULE_TPL_PROP === 6) {
        // 只读
        setTriggerObjModalFn(thisObj, triggerObj)
        resetNotRequiredRuleObj(thisObj, S_RULE_TPL_KEY)
      } else if (I_RULE_TPL_PROP === 7) {
        // 设置校验
        changeFormKeyRules(thisObj, S_RULE_TPL_KEY)
      } else if (I_RULE_TPL_PROP === 8) {
        // 解除校验,除必填外,其他的校验也要一并删除.
        clearFormRuleObj(thisObj, S_RULE_TPL_KEY)
      } else if (I_RULE_TPL_PROP === 41) {
        // 执行加载枚举事件
        changeLoadEnumList(thisObj, triggerObj, globalData)
      } else if (I_RULE_TPL_PROP === 50) {
        // 切换枚举
        changeActiveEnumKey(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 1) {
        changeShowActiveRowCol(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 2) {
        // 隐藏
        changeHiddenActiveRowCol(thisObj, S_RULE_TPL_KEY, globalData)
      } else if (I_RULE_TPL_PROP === 15) {
        // 显示或隐藏
        changeShowActiveRowCol(thisObj, S_RULE_TPL_KEY, globalData)
      }
    }
  } else if (I_TRIGGER_OBJ === 49) {
    // const record = Object.assign({}, thisObj.openParam, thisObj.initData, thisObj.formModel, thisObj.clickRow)
    // const triggerBtn = thisObj.initUiVal.toolbar.btnData[key]
    execClickBtn(thisObj, globalData, key)
  } else {
    console.log('未支持事件.', I_TRIGGER_OBJ)
  }
}

export function execClickBtn(thisObj, globalData, key) {
  const triggerBtn = thisObj.initUiVal.toolbar.btnData[key]
  if (triggerBtn.fnType === 1) {
    const newGlobalData = Object.assign({}, globalData)
    newGlobalData.clickItem = triggerBtn
    newGlobalData.formModel = thisObj.formModel
    newGlobalData.globalParam = getGlobalParam(thisObj)
    fetchPageData(thisObj, newGlobalData)
  } else if (triggerBtn.fnType === 2) {
    eventOpenGeneralModal(thisObj, triggerBtn, globalData)
  } else if (triggerBtn.fnType === 94 || triggerBtn.fnType === 95) {
    const newGlobalData = Object.assign({}, globalData)
    newGlobalData.clickItem = triggerBtn
    newGlobalData.formModel = thisObj.formModel
    newGlobalData.globalParam = getGlobalParam(thisObj)
    searchSessionFormData(thisObj, newGlobalData)
  } else if (triggerBtn.fnType === 96) {
    const newGlobalData = Object.assign({}, globalData)
    newGlobalData.clickItem = triggerBtn
    newGlobalData.formModel = thisObj.formModel
    newGlobalData.globalParam = getGlobalParam(thisObj)
    searchSessionTpl(thisObj, newGlobalData)
  } else {
    triggerBtnEvent(thisObj, triggerBtn, globalData).then((res) => {
      if (res.result) {
        thisObj.tableData = res.data
      } else {
        thisObj.$message.warning(res.resultTip)
      }
    })
  }
}

export function setTriggerObjModalFn(thisObj, triggerObj) {
  const I_RULE_TPL_PROP = triggerObj.i_RULE_TPL_PROP
  const S_RULE_TPL_KEY = triggerObj.s_RULE_TPL_KEY
  const tmpObj = {}
  if (I_RULE_TPL_PROP === 5) {
    // 可写
    tmpObj[S_RULE_TPL_KEY] = true
  } else if (I_RULE_TPL_PROP === 6) {
    // 只读
    tmpObj[S_RULE_TPL_KEY] = false
  }
  const cccc = Object.assign({}, thisObj.modalFn, tmpObj)
  thisObj.modalFn = Object.assign({}, cccc)
}
